package apiDemo.bigdecimal;

import java.math.BigDecimal;

/**
 * 想要精确计算小数要使用BigDecimal的字符串构造方法
 */

public class Demo02 {
  public static void main(String[] args) {
    BigDecimal bd1 = new BigDecimal("0.1");
    BigDecimal bd2 = new BigDecimal("0.3");
    System.out.println(bd1);
    System.out.println(bd2);
    BigDecimal res1 = bd1.add(bd2);
    System.out.println(res1);
    BigDecimal res2 = bd1.subtract(bd2);
    System.out.println(res2);
    BigDecimal res3 = bd1.multiply(bd2);
    System.out.println(res3);
    BigDecimal res4 = bd2.divide(bd1);
    System.out.println(res4);
    // 除不尽的时候
    BigDecimal res5 = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
    System.out.println(res5);
  }
}
